package com.longtengyuyun;

import com.sun.imageio.stream.StreamFinalizer;
import sun.security.util.AuthResources_it;

import java.util.concurrent.TimeUnit;


/**
 * @author ZhaoYunLong
 * @date 2022/3/14
 * @description
 */
public class TestOne {

    static class Account {
        String name;
        Double balance;

        public synchronized void setBalance(String name, Double balance) {
            this.name = name;
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.balance = balance;
        }


        public synchronized void setBalanceTwo(String name, Double balance) {
            this.name = name;
            try {
                this.balance = balance;
                Thread.sleep(2000);
                int a = 1 / 0;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public String getName() {
            return name;
        }

        public Double getBalance() {
            synchronized (TestOne.class) {
                return balance;
            }
        }

        public Double getBalanceTwo() {
            return balance++;
        }
    }


    public static void main(String[] args) {

        Account a = new Account();
        new Thread(() -> {
            a.setBalanceTwo("zhangsan", 100.0);
        }).start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(a.getBalanceTwo());

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(a.getBalanceTwo());

        new Thread(a::getBalance).start();
    }
}
